package com.oasystem.service;

import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;

import com.oasystem.dao.jdbc.MemberDAO;
import com.oasystem.dao.jdbc.ProfitDAO;
import com.oasystem.domain.Member;
import com.oasystem.domain.MemberSummary;
import com.oasystem.domain.PagingBean;
import com.oasystem.domain.SelledGoodsSearchResult;
import com.oasystem.utils.BackupFilesUtils;

@Transactional(readOnly = true)
public class MemberService {
	private MemberDAO memberDAO;
	private ProfitDAO profitDAO;

	public void setMemberDAO(MemberDAO memberDAO) {
		this.memberDAO = memberDAO;
	}

	@Transactional(readOnly = false, propagation = Propagation.SUPPORTS, rollbackFor = { Exception.class })
	public void insertMember(Member member) {
		memberDAO.insertMember(member);
		profitDAO.insertInitProfitMemberRecord(member.getId(),
				member.getProfitReturnAmount());
//		profitDAO.insertMemberShouldReturnAmountTodayRecord(member.getId());
	}

	@Transactional(readOnly = false, propagation = Propagation.SUPPORTS, rollbackFor = { Exception.class })
	public void updateMember(Member member) {
		memberDAO.updateMember(member);
	}

	public void getPagingMember(PagingBean<Member> pagingBean) {
		memberDAO.getPagingMember(pagingBean);
	}

	public void searchForPagingList(Member member, PagingBean<Member> pagingBean) {
		memberDAO.searchForPagingList(member, pagingBean);
	}


	public MemberSummary loadMemberSummary(String from, String to) {
		to = to + " 23:59:59";
		return memberDAO.loadSummary(from, to);
	}

	public void retrieveMemberGoodsCostPagingList(String from, String to,
			PagingBean<Member> pagingBean) {
		from = from + " 00:00:00";
		to = to + " 23:59:59";
		memberDAO.retrieveMemberGoodsCostPagingList(from, to, pagingBean);
	}

	@Transactional(readOnly = false, propagation = Propagation.SUPPORTS, rollbackFor = { Exception.class })
	public void deleteMember(Member member) {
		memberDAO.updateMember(member);
	}

	public void retrieveGoodsStatisticsList(String from, String to,
			PagingBean<SelledGoodsSearchResult> pagingBean) {
		from = from + " 00:00:00";
		to = to + " 23:59:59";
		memberDAO.retrieveGoodsStatisticsList(from, to, pagingBean);
		if (pagingBean.getData().size() == 1
				&& pagingBean.getData().get(0).getSelledGoodsCount()
						.equals("0")) {
			pagingBean.getData().remove(0);
		}
	}

	public void setProfitDAO(ProfitDAO profitDAO) {
		this.profitDAO = profitDAO;
	}

	public Member loadMember(Long memberID) {
		return memberDAO.loadMember(memberID);
	}
	
	@Transactional(readOnly = false, propagation = Propagation.SUPPORTS, rollbackFor = { Exception.class })
	public void deleteMember(Long memberID) {
		memberDAO.deleteMember(memberID);
	}

	@Transactional(readOnly = false, propagation = Propagation.SUPPORTS, rollbackFor = { Exception.class })
	public void deleteOldMemberData(String dateTime) {
		memberDAO.deleteOldMemberData(dateTime);
	}

	public void deleteBackupFiles(String dateTime) throws Exception {
		BackupFilesUtils.deleteBackupFiles(dateTime);
	}

}
